--[[
Name: BrownieHelper
Developed by: Sonora (The Dragon Flight)
Website: http://sonora.kirintor.googlepages.com/contents
SVN: http://code.google.com/p/browniehelper/source/browse
License: GNU General Public License v3
]]

function ViewRunsFrame:Initialize(app)
    --app is the brownie helper app
    
    --Save the brownie helper app
    self.app = app
    
    --Setup the point pools dropdown list width
    ViewRunsPointPoolDropdownMiddle:SetWidth(205)
    
    --Setup a single column table to use as our run selection buttons
    self.col_name = "Select a run"
    ViewRunsRunTable.candeselect = false
    ViewRunsRunTable:setFields({self.col_name,})
    self:updateRunList()
    
    --By default disable the update button
    ViewRunsUpdateButton:Disable()
    
    --Register a selection handler
    ViewRunsRunTable:registerSelectionCallback(function(row, selected) self:runSelected(row, selected) end)
    ViewRunsPointPoolDropdown:registerSelectionCallback(function() self:setPointPool() end)
    
    --Update our drop down list anytimes th points database changes
    self.app:registerEvent("BrownieHelper_PointsChanged", function() self:updateDropdownList() end)
    
    --Register a double click handler
    ViewRunsRunTable:registerDoubleClickCallback(function(row) if ViewRunsRunTable:getSelections()[1] then self:OnClose() end end)
    
end

function ViewRunsFrame:updateDropdownList()
    ViewRunsPointPoolDropdown:setOptions(keys(self.app:getPointsDb()))
    if ViewRunsRunTable:getSelections()[1] then ViewRunsPointPoolDropdown:setSelected(ViewRunsRunTable:getSelections()[1].run_info.point_pool) end
end

function ViewRunsFrame:updateRunList()

    --Setup the point pools dropdown list
    ViewRunsFrame:updateDropdownList()

    ViewRunsRunTable:clearRows()
    for run_name, run_info in pairs(self.app:getRunDb()) do
        --For each run in the database
        
        local isselected = run_name==self.app:getCurrentRun()
        local row = {
            [self.col_name] = run_name,
            ["wiTableSelected"] = isselected,
            ["run_info"] = run_info,
        }
        
        ViewRunsRunTable:addRow(row)
        
        if isselected then
            --Call runSelected with the currently selected run, to set us up
            self:runSelected(row, true)
        end
        
    end

end

function ViewRunsFrame:runSelected(row, selected)

    if selected==true then
        --Notify the application
        self.app:setCurrentRun(row[self.col_name])
        
        --Setup the right panel for editing
        ViewRunsPointPoolDropdown:setSelected(row.run_info.point_pool)
        ViewRunsNoteEditBox:SetText((row.run_info.note or ""))
        ViewRunsStartTimeEditBox:SetText(row.run_info.raidStartTime or "")
        
    else
        --Notify the application
        self.app:setCurrentRun(nil)
        
        --Setup the right panel for editing
        ViewRunsPointPoolDropdown:setSelected(1)
        ViewRunsNoteEditBox:SetText("")
        ViewRunsStartTimeEditBox:SetText("")
        
        --Enable the update button
        ViewRunsUpdateButton:Disable()
        
    end
    
end

--Event handlers
function ViewRunsFrame:OnAddRun()

    if #keys(self.app:getPointsDb())<1 then
        inputDialog(
            "You can't add a new run right now because there are no point pools setup.  Would you like to open the addon configuration panel?",
            function(response)
                if response then self.app:showConfiguration() end
            end
        )
        return false
    end

    local zone = GetZoneText()
    local default_text = string.sub(date(), 1, 8).." "..zone --just grab the date portion of the date string
    
    local pool_names = keys(self.app:getPointsDb())
    local default_pool = ""
    if has(pool_names, zone) then
        default_pool = zone
    else
        default_pool = ViewRunsPointPoolDropdown:getSelected()
    end
    
    local callback = function(response)
        self.app:getRunDb()[response] = {["note"]="",["point_pool"]=default_pool, ["raidStartTime"]=(self.app:getDb().Options.DefaultRaidStartTime or "00:01")}
        self.app:setCurrentRun(response)
        self:updateRunList()
    end

    inputDialog("Enter a name for the new run:", callback, default_text)
    
end

function ViewRunsFrame:OnRemoveRun()

    for i,row in pairs(ViewRunsRunTable:getSelections()) do --should only be one
        callback = function(response)
            if response==true then
                self:runSelected(row,false)
                self.app:getRunDb()[row[self.col_name]] = nil --a delete in lua
                self:updateRunList()
            end
        end
        
        yesnoDialog("Are you sure you want to delete this run from the database?  (all event info will also be lost for it)", callback)
        
    end
    
end

function ViewRunsFrame:OnClose()
    self:Hide()
end

function ViewRunsFrame:setPointPool()

    if ViewRunsRunTable:getSelections()[1] then
        local row = ViewRunsRunTable:getSelections()[1]
        local run_name = row[self.col_name]
        local db = self.app:getRunDb()[run_name]
        
        --Update the database
        db.point_pool = ViewRunsPointPoolDropdown:getSelected()
        db.note = ViewRunsNoteEditBox:GetText()
        db.raidStartTime = ViewRunsStartTimeEditBox:GetText()
        
        self.app:setCurrentRun(run_name)
        
    end

end

function ViewRunsFrame:OnNoteChar()

    if ViewRunsRunTable:getSelections()[1] then
        --Enable the update button
        ViewRunsUpdateButton:Enable()
    end
    
end

function ViewRunsFrame:OnStartTimeChar()

    if ViewRunsRunTable:getSelections()[1] then
        --Enable the update button
        ViewRunsUpdateButton:Enable()
    end

end

function ViewRunsFrame:OnUpdate()

    local row = ViewRunsRunTable:getSelections()[1]
    local run_name = row[self.col_name]
    local db = self.app:getRunDb()[run_name]
    
    --Update the database
    db.note = ViewRunsNoteEditBox:GetText()
    db.raidStartTime = ViewRunsStartTimeEditBox:GetText()
    
    ViewRunsUpdateButton:Disable()
    
end
